package com.java.module.chat.service.impl;

import java.util.Date;
import java.util.List;

import com.java.common.lang.domain.entity.User;
import com.java.common.service.ApiTokenService;
import com.java.common.utils.AssertKit;
import com.java.common.utils.DateUtils;
import com.java.module.chat.domain.GroupUser;
import com.java.module.chat.service.IGroupUserService;
import com.java.module.chat.service.ISessionService;
import com.java.module.chat.domain.ChatGroup;
import com.java.module.chat.mapper.ChatGroupMapper;
import com.java.module.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.java.module.chat.service.IChatGroupService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 聊天模块：群聊Service业务层处理
 * 
 * @author Administrator
 * @date 2023-02-16
 */
@Service
public class ChatGroupServiceImpl implements IChatGroupService 
{
    @Autowired
    private ChatGroupMapper chatGroupMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private IGroupUserService groupUserService;

    @Autowired
    private ISessionService sessionService;

    @Autowired
    private ApiTokenService tokenService;

    /**
     * 查询聊天模块：群聊
     * 
     * @param groupId 聊天模块：群聊ID
     * @return 聊天模块：群聊
     */
    @Override
    public ChatGroup selectChatGroupById(Long groupId)
    {
        return chatGroupMapper.selectChatGroupById(groupId);
    }

    /**
     * 查询聊天模块：群聊列表
     * 
     * @param chatGroup 聊天模块：群聊
     * @return 聊天模块：群聊
     */
    @Override
    public List<ChatGroup> selectChatGroupList(ChatGroup chatGroup)
    {
        return chatGroupMapper.selectChatGroupList(chatGroup);
    }

    /**
     * 新增聊天模块：群聊
     * 
     * @param chatGroup 聊天模块：群聊
     * @return 结果
     */
    @Override
    public int insertChatGroup(ChatGroup chatGroup)
    {
        chatGroup.setCreateTime(DateUtils.getNowDate());
        return chatGroupMapper.insertChatGroup(chatGroup);
    }

    /**
     * 修改聊天模块：群聊
     * 
     * @param chatGroup 聊天模块：群聊
     * @return 结果
     */
    @Override
    public int updateChatGroup(ChatGroup chatGroup)
    {
        chatGroup.setUpdateTime(DateUtils.getNowDate());
        return chatGroupMapper.updateChatGroup(chatGroup);
    }

    /**
     * 批量删除聊天模块：群聊
     * 
     * @param groupIds 需要删除的聊天模块：群聊ID
     * @return 结果
     */
    @Override
    public int deleteChatGroupByIds(Long[] groupIds)
    {
        return chatGroupMapper.deleteChatGroupByIds(groupIds);
    }

    /**
     * 删除聊天模块：群聊信息
     * 
     * @param groupId 聊天模块：群聊ID
     * @return 结果
     */
    @Override
    public int deleteChatGroupById(Long groupId)
    {
        return chatGroupMapper.deleteChatGroupById(groupId);
    }


    /**
     * 创建群聊
     * @param loginUserId 登录用户id
     * @param uids 群成员id
     * @return 创建的群聊id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createGroup(Long loginUserId, Long[] uids) {
        ChatGroup chatGroup = new ChatGroup();
        chatGroup.setUid(loginUserId);
        String groupNickname = userMapper.getGroupNickname(uids);
        User user = userMapper.selectUserById(loginUserId);
        String nickname = user.getNickname();
        chatGroup.setName(nickname + "," + groupNickname);
        chatGroup.setAvatar(user.getAvatar());
        int i = insertChatGroup(chatGroup);
        GroupUser groupUser = new GroupUser();
        groupUser.setGid(chatGroup.getGroupId());
        groupUser.setIdentity(1);
        groupUser.setUid(loginUserId);
        groupUserService.insertGroupUser(groupUser);
        sessionService.addSession( chatGroup.getGroupId(), 2,loginUserId,0);
        for (Long uid : uids) {
            groupUser.setIdentity(0);
            groupUser.setUid(uid);
            groupUserService.insertGroupUser(groupUser);
            sessionService.addSession(chatGroup.getGroupId(),2,uid,0);
        }
        return chatGroup.getGroupId();
    }

    /**
     * 修改群聊名称
     * @param uid
     * @param gid
     * @param name
     * @return
     */
    @Override
    public int changeGroupName(Long uid, Long gid, String name) {
        ChatGroup chatGroup = chatGroupMapper.selectChatGroupById(gid);
        AssertKit.notNull(chatGroup,"信息错误");
        AssertKit.isTrue(chatGroup.getUid().equals(uid),"不是管理员，无法修改");
        chatGroup.setName(name);
        chatGroup.setUpdateTime(new Date());
        return chatGroupMapper.updateChatGroup(chatGroup);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addGroupUser(Long loginUserId, Long gid, String idNumber) {
        AssertKit.notNull(idNumber,"用户名未填");
        AssertKit.notNull(gid,"信息错误");
        User user = userMapper.selectUserByIdNumber(idNumber);

        AssertKit.notNull(user,"用户不存在");
        ChatGroup chatGroup = chatGroupMapper.selectChatGroupById(gid);
        AssertKit.notNull(chatGroup,"信息错误");
        AssertKit.isTrue(chatGroup.getUid().equals(loginUserId),"不是管理员");

        GroupUser groupUser = new GroupUser();
        groupUser.setGid(gid);
        groupUser.setIdentity(0);
        groupUser.setUid(user.getUid());

        sessionService.addSession(gid,2,user.getUid(),0);
        return  groupUserService.insertGroupUser(groupUser);
    }
}
